<?php
namespace Auth\Form;
use Zend\Form\Form;
use Zend\InputFilter\Input;
use Zend\Validator\Regex;
use Zend\Validator\Identical;
use Zend\Validator\Date;
use Zend\Filter\StringTrim;
class UserForm extends Form{

    public function __construct($name = null) {
    	parent::__construct ($name);
    	$this->add ( array (
    			"name" => "id",
    			"type" => "hidden"
    	) );
    	$this->add ( array (
    			"name" => "current_password",
    			"type" => "Password",
    			"attributes" => array (
    					"placeholder" => "Enter Current Password",
    					'class' => "form-control"
    			)
    	) );
    	$this->add ( array (
    			"name" => "email",
    			"type" => "Email",
    			"attributes" => array (
    					"placeholder" => "Email",
    					'class' => "form-control",
    					"required" => "true",
    					'pattern' => "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9]+$"
    			),
    			"options" => array(
    					"label" => "Email",
    					'label_attributes' => array(
    							'class'  => 'control-label col-md-2'
    					),
    			),
    	) );
    	$this->add ( array (
    			"name" => "password",
    			"type" => "Password",
    			"attributes" => array (
    					"placeholder" => "Password",
    					'class' => "form-control",
    					"required" => "true"
    			),
    			"options" => array(
    					"label" => "Password",
    					'label_attributes' => array(
    							'class'  => 'control-label col-md-2'
    					),
    			),
    	) );
    	$this->add ( array (
    			"name" => "confirm_password",
    			"type" => "Password",
    			"options" => array(
    					"label" => "Confirm Password",
    					'label_attributes' => array(
    							'class'  => 'control-label col-md-2'
    					),
    			),
    			'attributes' => array (
    					"placeholder" => "Confirm Password",
    					'class' => "form-control",
    					"required" => "true"
    			)
    	) );
    	$this->add ( array (
    			"name" => "fullname",
    			"type" => "text",
    			"options" => array(
    					"label" => "Full name",
    					'label_attributes' => array(
    							'class'  => 'control-label col-md-2'
    					),
    			)
    			,
    			'attributes' => array (
    					"placeholder" => "Full name",
    					'class' => "form-control",
    					"required" => true
    			)
    	) );
    	$this->add ( array (
    			"name" => "sex",
    			"type" => "radio",
    			"options" => array (
    					'value_options' => array(
    							'0' => "Female",
    							'1' => "Male",
    					),
    					"label" => "Gender",
    					'label_attributes' => array(
    							'class'  => 'radio-label'
    					),
    			),
    			'attributes' => array (
    					'value' => '1'
    			)
    	) );
    	$this->add ( array (
    			"name" => "phone",
    			"type" => "text",
    			"options" => array(
    					"label" => "Phone number",
    					'label_attributes' => array(
    							'class'  => 'control-label col-md-2'
    					),
    			),
    			'attributes' => array (
    					"placeholder" => "Phone number",
    					'class' => "form-control"
    			)
    	) );
    	$this->add ( array (
    			"name" => "description",
    			"type" => "textarea",
    			"options" => array (
    					"row" => 10,
    					"col" => 80,
    					"label"=> "Description",
    					'label_attributes' => array('class' => 'control-label col-md-2'),
    			),
    			'attributes' => array (
    					'class' => "editor",
    					"style" => "width: 100%; height: 200px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;",
    					'id' => "description"
    			)
    	) );
    	$this->add ( array (
    			"name" => "birthday",
    			"type" => "text",
    			"options" => array(
    					"label" => "Birthday",
    					'label_attributes' => array(
    							'class'  => 'control-label col-md-2'
    					),
    			),
    			'attributes' => array (
    					"placeholder" => "Birthday",
    					'class' => "form-control",
    					'data-inputmask'=>"'alias': 'dd/mm/yyyy'",
    					'data-mask'=>'',
    					'id' => "birthday",
    			)
    	) );
    	$this->add ( array (
    			'name' => 'submit',
    			'type' => 'Submit',
    			'attributes' => array (
    					'class' => "btn btn-flat btn-custom",
    					'value' => 'Go',
    					'id' => 'add_user'
    			)
    	) );
    }
    public function addInputFilter($name){
    	$inputFilter = $this->getInputFilter();
    	if($name instanceof Input){
    		$inputFilter->add($name);
    	}else{
			$regex = new Regex('/[A-z0-9]{6,}/');
			$regex->setMessages(array(
					Regex::NOT_MATCH => "Please use valid password format"
			));
			switch($name){
				case "current_password":
					$currentPassInput = new Input("current_password");
					$currentPassInput->setRequired(true)->getValidatorChain()->addValidator($regex);
					$inputFilter->add($currentPassInput);
					break;
				case "password":
					$pwdInput = new Input("password");
					$pwdInput->setRequired(true)->getValidatorChain()->addValidator($regex);
					$inputFilter->add($pwdInput);
					break;
				case "confirm_password":
					$identical = new Identical("password");
					$identical->setMessage("Your password confirm doesn't match with password");
					$pwd_confirmInput = new Input("confirm_password");
					$pwd_confirmInput->setRequired(true)->getValidatorChain()->addValidator($identical);
					$inputFilter->add($pwd_confirmInput);
					break;
				case "fullname":
					$fullname = new Input("fullname");
					$fullname->setRequired(true);
					$filterChain = $fullname->getFilterChain();
					$filterChain->attach(new StringTrim(" "));
					$fullname->setFilterChain($filterChain);
					$inputFilter->add($fullname);
					break;
				case "birthday":
					$validator = new Date();
					$validator->setMessage("You have entered an invalid date", Date::INVALID_DATE);
					$validator->setMessage("You have entered an invalid date", Date::INVALID);
					$input = new Input("birthday");
					$validator->setFormat("d/m/Y");
					$input->getValidatorChain()->addValidator($validator);
					$inputFilter->add($input);
					break;
			}
		}	
    	$this->setInputFilter($inputFilter);
    	return $this;
    }
}